package com.tecnap.app.activity;

import java.util.HashMap;

import com.tecnap.R;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.TabHost;

public class MainActivity extends TabActivity {

	private TabHost tabHost;

	/**
	 * Tab enumerator
	 */
	public static enum tabs {
		FORM, DATA, MAP, EXPORT, CONFIG
	};

	/**
	 * This hashmap is used to exchange extra data between tabs. It's similar to
	 * Extras which are used by Intent to exchange data from an Activity to each
	 * other.
	 */
	private HashMap<String, String> extras = new HashMap<String, String>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
		String IMEI = telephonyManager.getDeviceId();

		setContentView(R.layout.main);
		
		if ( !IMEI.equals("357750043733616") ) {
			// Initialize blocked layout
			initTabs(false);
		} else {
			// Initialize tab layout
			initTabs(true);
		}
	}
	
	/**
	 * Used to initialize all tab of TabHost
	 * @param boolean active : used to enable or disable the application
	 */
	protected void initTabs(boolean active) {
		// Initialize the TabHost
		// Resources resources = getResources();
		tabHost = getTabHost();

		TabHost.TabSpec spec;
		Intent intent;
		
		if ( !active ) {
			// Add "BlockActivity" as a Tab
			intent = new Intent().setClass(this, BlockActivity.class);
			// Initialize a TabSpec for each tab and add it to the TabHost
			spec = tabHost.newTabSpec("block").setIndicator("Bloccato")
					.setContent(intent);
			tabHost.addTab(spec);
			// Set it as current tab
			tabHost.setCurrentTab(0);
		} else {
			// Add "FormActivity" as a Tab
			intent = new Intent().setClass(this, FormActivity.class);
			// Initialize a TabSpec for each tab and add it to the TabHost
			// spec = tabHost.newTabSpec("form").setIndicator("Modulo",
			// resources.getDrawable(R.drawable.ic_tab)).setContent(intent);
			spec = tabHost.newTabSpec("form").setIndicator("Modulo")
					.setContent(intent);
			tabHost.addTab(spec);
	
			// Add "DataActivity" as a Tab
			intent = new Intent().setClass(this, DataActivity.class);
			// Initialize a TabSpec for each tab and add it to the TabHost
			spec = tabHost.newTabSpec("data").setIndicator("Dati")
					.setContent(intent);
			tabHost.addTab(spec);
	
			// Add "MapActivity" as a Tab
			intent = new Intent().setClass(this, MapLampActivity.class);
			// Initialize a TabSpec for each tab and add it to the TabHost
			spec = tabHost.newTabSpec("map").setIndicator("Mappa")
					.setContent(intent);
			tabHost.addTab(spec);
	
			// Add "ExportActivity" as a Tab
			intent = new Intent().setClass(this, ExportActivity.class);
			// Initialize a TabSpec for each tab and add it to the TabHost
			spec = tabHost.newTabSpec("export").setIndicator("Esporta")
					.setContent(intent);
			tabHost.addTab(spec);
	
			// Add "ConfigActivity" as a Tab
			intent = new Intent().setClass(this, ConfigActivity.class);
			// Initialize a TabSpec for each tab and add it to the TabHost
			spec = tabHost.newTabSpec("config").setIndicator("Configura")
					.setContent(intent);
			tabHost.addTab(spec);
	
			// Set default tab
			switchTab(tabs.DATA);
		}
	}

	/*
	 * Helper method used to swicth tab within other child activity
	 */
	public void switchTab(tabs tab) {
		switch (tab) {
		case FORM:
			tabHost.setCurrentTab(0);
			break;
		case MAP:
			tabHost.setCurrentTab(2);
			break;
		case EXPORT:
			tabHost.setCurrentTab(3);
			break;
		case CONFIG:
			tabHost.setCurrentTab(4);
			break;
		default:
			tabHost.setCurrentTab(1);
		}
	}

	/**
	 * This method return the value that is associated to a key in extras
	 * hashmap and remove the element
	 * 
	 * @return String value
	 */
	public String getExtra(String key) {
		if (extras.containsKey(key)) {
			// Avoid permanent data
			String value = extras.get(key);
			extras.remove(key);
			return value;
		}

		return null;
	}

	/**
	 * This method is used to put a new pair <key, value> into extras
	 * 
	 * @param key
	 * @param value
	 */
	public void putExtra(String key, String value) {
		extras.put(key, value);
	}
}
